<template lang="html">
    <div id="subtmpl">
		<div class="inleft div" @click="subtract">-</div>
		<div class="incenter div" v-text="count"></div>
		<div class="inright div"  @click="add">+</div>
	</div>
</template>

<script>
export default {
    props: ['initCount', 'goodsid'],
    data(){
        return {
            count: 1, //代表商品购买数量
            resObj: {goodsid: 0, type: 'add'}
        }
    },
    created(){
        this.count = this.initCount;
    },
    methods: {
        subtract(){
            this.count--;
            //确保最小值为1
            if(this.count < 1){
                this.count = 1;
            }
            this.sendmessage('subtract');
        },
        add(){
            this.count++;
            this.sendmessage('add');
        },
        sendmessage(type){
            this.resObj.type = type;
            this.resObj.goodsid = this.goodsid;
            this.$emit('cardataobj',this.resObj);
        }
    }
}
</script>

<style lang="css" scoped>
#subtmpl .div{
		width: 40px;
		height: 25px;
		line-height: 25px;
		border: 1px solid #000;
		display: inline-block;
		text-align: center;
	}
</style>
